package com.ivan.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ivan.commonutils.CommonResult;
import com.ivan.eduservice.entity.EduTeacher;
import com.ivan.eduservice.service.EduTeacherService;
import com.ivan.eduservice.vo.TeacherQuery;
import com.ivan.servicebase.exceptionhandler.IvanException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Ivan
 * @since 2020-07-19
 */
@Api("讲师管理")//swagger中的controller提示
@RestController//使用rest风格
@RequestMapping("/eduservice/teacher")
@Slf4j//使用日志功能
@CrossOrigin//允许跨域
public class EduTeacherController {

    //注入service
    @Autowired
    private EduTeacherService teacherService;

    /**
     * @author Ivan
     * @date 2020/7/19 20:28
     * @Description: 查询所有讲师数据
     */
    @ApiOperation(value = "查询所有讲师数据——测试时使用")
    @GetMapping("/findAll")
    public CommonResult findAllTeacher(){
        log.info("*********************************这是我自定义的日志信息*********************************");
        //模拟异常
//        try {
//            int i = 10 / 0;
//        } catch (Exception e) {
//            throw new IvanException(500,"这是我自定义的异常...");
//        }
        //调用service方法查询所有讲师数据
        List<EduTeacher> list = teacherService.list(null);
        return CommonResult.success().data("list", list);
    }

    /**
     * @author Ivan
     * @date 2020/7/20 22:20
     * @Description: 分页查询讲师数据
     */
    @ApiOperation(value = "分页查询讲师数据——不用这个方法了，使用下面那个带条件查询的")
    @PostMapping("/pageQueryTeacher/{page}/{limit}")
    public CommonResult pageQueryTeacher(@PathVariable("page")Long page,
                                         @PathVariable("limit")Long limit){
        //创建分页查询对象
        Page<EduTeacher> pageTeacher = new Page<>(page,limit);
        //分页查询查询数据
        IPage<EduTeacher> list = teacherService.page(pageTeacher, null);

        //总记录数
        long total = pageTeacher.getTotal();
        //数据集合
        List<EduTeacher> records = pageTeacher.getRecords();

        //返回数据
//        Map map = new HashMap();
//        map.put("total", total);
//        map.put("records", records);
//        return CommonResult.success().data(map);

        //链式编程
        return CommonResult.success().data("total", total).data("records", records);
    }

    //************************************************************正式接口************************************************************

    /**
     * @author Ivan
     * @date 2020/7/19 20:37
     * @Description: 根据id删除讲师数据
     * @ApiParam swagger中的参数提示
     */
    @ApiOperation(value = "根据id删除讲师数据")
    @DeleteMapping("/{id}")
    public CommonResult removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id")String id){
        boolean flag = teacherService.removeById(id);
        if (flag){
            return CommonResult.success();
        } else {
            return CommonResult.fail();
        }
    }


    /**
     * @author Ivan
     * @date 2020/7/20 22:21
     * @Description: 条件查询带分页的方法
     */
    @ApiOperation(value = "条件查询带分页的方法")
    @PostMapping("pageQueryTeacherCondition/{page}/{limit}")
    public CommonResult pageTeacherCondition(@PathVariable("page")Long page,
                                            @PathVariable("limit")Long limit,
                                            @RequestBody(required = false) TeacherQuery teacherQuery) {
        //创建分页查询对象
        Page<EduTeacher> pageTeacher = new Page<>(page,limit);

        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        // mybatis学过 动态sql
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(name)) {
            //构建条件
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)) {
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create",end);
        }

        //按时间倒序排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        teacherService.page(pageTeacher,wrapper);

        //总记录数
        long total = pageTeacher.getTotal();
        //数据list集合
        List<EduTeacher> records = pageTeacher.getRecords();
        //链式编程
        return CommonResult.success().data("total",total).data("records",records);
    }

    /**
     * @author Ivan
     * @date 2020/7/20 22:43
     * @Description: 添加讲师接口的方法
     */
    @ApiOperation(value = "添加讲师接口的方法")
    @PostMapping("addTeacher")
    public CommonResult addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        if(save) {
            return CommonResult.success();
        } else {
            return CommonResult.fail();
        }
    }

    /**
     * @author Ivan
     * @date 2020/7/20 22:38
     * @Description: 根据讲师id进行查询
     */
    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("getTeacherById/{id}")
    public CommonResult getTeacherById(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return CommonResult.success().data("teacher",eduTeacher);
    }

    /**
     * @author Ivan
     * @date 2020/7/20 22:38
     * @Description: 讲师修改功能
     */
    @ApiOperation(value = "讲师修改功能")
    @PostMapping("updateTeacher")
    public CommonResult updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        if(flag) {
            return CommonResult.success();
        } else {
            return CommonResult.fail();
        }
    }

}

